<?php
namespace Home\Controller;
use Think\Controller;
// include '__PUBLIC__/config.php';
class AliController extends Controller {

    // 初始化
    public function _initialize()
    {    header("Content-type: text/html; charset=utf-8");
        vendor('ali.f2fpay.model.builder.AlipayTradePrecreateContentBuilder');
                vendor('ali.f2fpay.model.builder.AlipayTradePayContentBuilder');

        vendor('ali.f2fpay.service.AlipayTradeService');
        vendor('ali.f2fpay.model.builder.AlipayTradeRefundContentBuilder');//引入退款文件
    }

     //二维码的生成
         // 二维码的生成
    public function index(){

      if (isset($_GET['id']) && isset($_GET['mac_id'])) {
        $pay_way='支付宝';
        $out_trade_no= $this->order_add($_GET['id'],$_GET['mac_id'],$pay_way);//传参产生订单号  
          if ($out_trade_no!=0) {
            $config=C('config');
            $subject ='自助售货机';
            // $totalAmount = $_POST['total_amount'];
             $totalAmount ='0.01';
            // 支付超时，线下扫码交易定义为5分钟
            $timeExpress = "1m";
            // 创建请求builder，设置请求参数
            $qrPayRequestBuilder = new \AlipayTradePrecreateContentBuilder();
            $qrPayRequestBuilder->setOutTradeNo($out_trade_no);
            $qrPayRequestBuilder->setTotalAmount($totalAmount);
            $qrPayRequestBuilder->setTimeExpress($timeExpress);
            $qrPayRequestBuilder->setSubject($subject);
            // 调用qrPay方法获取当面付应答
            $qrPay = new \AlipayTradeService($config);
            $qrPayResult = $qrPay->qrPay($qrPayRequestBuilder);
            //  根据状态值进行业务处理
            switch ($qrPayResult->getTradeStatus()){
                case "SUCCESS":
                    $response = $qrPayResult->getResponse();
                    $qrcode = $qrPay->create_erweima($response->qr_code);
                    $url=$response->qr_code;
                  $arr=array(
                    'order'=>$out_trade_no,
                     'url'=>$url,
                    );
                  echo json_encode($arr);

                    // $this->assign('out_trade_no',$out_trade_no);
                    // $this->assign('qrcode',$url);
                    // $this->display();
                    break;
                case "FAILED":
                    echo "支付宝创建订单二维码失败!!!"."<br>--------------------------<br>";
                    if(!empty($qrPayResult->getResponse())){
                        print_r($qrPayResult->getResponse());
                    }
                    break;

             }
             return ;
          }else{
             echo "";
          }  
        }
    }

    // 条形码的生成
    public function tiao(){
         
        
             $config=C('config');
                // var_dump($config);
                // 需保证商户系统端不能重复，建议通过数据库sequence生成，
            $outTradeNo = "qrpay".date('Ymdhis').mt_rand(100,1000);

            // $subject = $_POST['subject'];
            $subject ='自助售货机支付';
            
             $totalAmount = $_POST['total_amount'];
             //$totalAmount ='0.05';
               $authCode ='285517079184648919'; //28开头18位数字
               $authCode=$_POST['auth_code'];
            // 支付超时，线下扫码交易定义为5分钟
            $timeExpress = "5m";


          // 创建请求builder，设置请求参数
       $barPayRequestBuilder = new \AlipayTradePayContentBuilder();
       $barPayRequestBuilder->setOutTradeNo($outTradeNo);
        $barPayRequestBuilder->setTotalAmount($totalAmount);
        $barPayRequestBuilder->setAuthCode($authCode);
         $barPayRequestBuilder->setTimeExpress($timeExpress);
      $barPayRequestBuilder->setSubject($subject);
      // 调用barPay方法获取当面付应答
     $barPay = new \AlipayTradeService($config);
      $barPayResult = $barPay->barPay($barPayRequestBuilder);
 
switch ($barPayResult->getTradeStatus()) {
 case "SUCCESS":
  echo "支付宝支付成功:" . "<br>--------------------------<br>";
  print_r($barPayResult->getResponse());
  break;
 case "FAILED":
  echo "支付宝支付失败!!!" . "<br>--------------------------<br>";
  if (!empty($barPayResult->getResponse())) {
   print_r($barPayResult->getResponse());
  }
  break;
 case "UNKNOWN":
  echo "系统异常，订单状态未知!!!" . "<br>--------------------------<br>";
  if (!empty($barPayResult->getResponse())) {
   print_r($barPayResult->getResponse());
  }
  break;
 default:
  echo "不支持的交易状态，交易返回异常!!!";
  break;
}
 return;

    }
    

    // 支付查询
    public  function  orderQuery(){
          
      if (!empty($_GET['out_trade_no'])&& trim($_GET['out_trade_no'])!=""){
            ////获取商户订单号
            $out_trade_no = trim($_GET['out_trade_no']);
             $config=C('config');
            //第三方应用授权令牌,商户授权系统商开发模式下使用
            // $appAuthToken = "";//根据真实值填写

            //构造查询业务请求参数对象
            $queryContentBuilder = new \AlipayTradeQueryContentBuilder();
            $queryContentBuilder->setOutTradeNo($out_trade_no);

            $queryContentBuilder->setAppAuthToken($appAuthToken);


            //初始化类对象，调用queryTradeResult方法获取查询应答
            $queryResponse = new \AlipayTradeService($config);
            $queryResult = $queryResponse->queryTradeResult($queryContentBuilder);

            //根据查询返回结果状态进行业务处理
            switch ($queryResult->getTradeStatus()){
                case "SUCCESS":
                    echo "支付宝查询交易成功:"."<br>--------------------------<br>";
                    print_r($queryResult->getResponse());
                    break;
                case "FAILED":
                    echo "支付宝查询交易失败或者交易已关闭!!!"."<br>--------------------------<br>";
                    if(!empty($queryResult->getResponse())){
                        print_r($queryResult->getResponse());
                    }
                    break;
                case "UNKNOWN":
                    echo "系统异常，订单状态未知!!!"."<br>--------------------------<br>";
                    if(!empty($queryResult->getResponse())){
                        print_r($queryResult->getResponse());
                    }
                    break;
                default:
                    echo "不支持的查询状态，交易返回异常!!!";
                    break;
               }
            return ;
         }
    }
    
  
    // 退款
    public  function   orderRefund(){
               if (!empty($_GET['out_trade_no'])&& trim($_GET['out_trade_no'])!=""){
                $config=C('config');
                $out_trade_no = trim($_GET['out_trade_no']);
                $refund_amount = '0.01';
                $out_request_no = time().rand(10,100);
                
                //第三方应用授权令牌,商户授权系统商开发模式下使用
                // $appAuthToken = "";//根据真实值填写
                
                //创建退款请求builder,设置参数
                $refundRequestBuilder = new \AlipayTradeRefundContentBuilder();
                    $refundRequestBuilder->setOutTradeNo($out_trade_no);
                    $refundRequestBuilder->setRefundAmount($refund_amount);
                    $refundRequestBuilder->setOutRequestNo($out_request_no);

                    // $refundRequestBuilder->setAppAuthToken($appAuthToken);

                //初始化类对象,调用refund获取退款应答
                $refundResponse = new \AlipayTradeService($config);
                $refundResult = $refundResponse->refund($refundRequestBuilder);

                //根据交易状态进行处理
                switch ($refundResult->getTradeStatus()){
                    case "SUCCESS":
                        echo "支付宝退款成功:"."<br>--------------------------<br>";
                        print_r($refundResult->getResponse());
                        break;
                    case "FAILED":
                        echo "支付宝退款失败!!!"."<br>--------------------------<br>";
                        if(!empty($refundResult->getResponse())){
                            print_r($refundResult->getResponse());
                        }
                        break;
                    case "UNKNOWN":
                        echo "系统异常，订单状态未知!!!"."<br>--------------------------<br>";
                        if(!empty($refundResult->getResponse())){
                            print_r($refundResult->getResponse());
                        }
                        break;
                    default:
                        echo "不支持的交易状态，交易返回异常!!!";
                        break;
                }
                return ;
            }



    }


}